package com.sh.d1.gatewayserver;


import com.sh.d1.gameserver.msg.GameMsgProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 验证用户id
 */
public class ValidateUserIdHandler extends ChannelInboundHandlerAdapter {
    static private final Logger LOGGER = LoggerFactory.getLogger(ValidateUserIdHandler.class);


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msgObj) throws Exception {
        if (null == ctx ||
                !(msgObj instanceof BinaryWebSocketFrame)) {
            return;
        }
        BinaryWebSocketFrame inputFrame = (BinaryWebSocketFrame) msgObj;
        ByteBuf bytebuf = inputFrame.content();

        //读掉消息长度
        bytebuf.readShort();
        //读取消息编码
        int msgCode = bytebuf.readShort();

        if (GameMsgProtocol.MsgCode.USER_LOGIN_CMD_VALUE != msgCode &&
                IdSetterGetter.getUserId(ctx.channel()) <= 0) {
            LOGGER.warn("尚未完成登录过程,用户 id 为空");
            return;
        }
        bytebuf.resetReaderIndex();
        ctx.fireChannelRead(msgObj);
    }
}
